package com.gxa.modules.service.operationsservice.impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.operationsmapper.ContentMapper;
import com.gxa.modules.pojo.Comment;
import com.gxa.modules.pojo.Content;
import com.gxa.modules.pojo.Contract;
import com.gxa.modules.service.operationsservice.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {

    @Autowired(required = false)
    ContentMapper contentMapper;
    @Override
    public R selectContent(int page, int size, String title, String contentType) {
        R r = new R();
        int index = (page-1)*size;
        List<Content> list = contentMapper.selectContent(index, size, title, contentType);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(contentMapper.selectCountContent(title, contentType));
        r.setData(list);
        return r;
    }

    @Override
    public R selectComment(int page, int size, String title, String contentType) {
        R r = new R();
        int index = (page-1)*size;
        List<Comment> list = contentMapper.selectComment(index, size, title, contentType);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(contentMapper.selectCountContent(title, contentType));
        r.setData(list);
        return r;
    }

    @Override
    public R insertContent(Content content) {
        R r = new R();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        content.setAddTime(simpleDateFormat.format(date));
        int i = contentMapper.insertContent(content);
        if(i>0)
        {
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }

    @Override
    public R selectContentById(int id) {
        R r= new R();
        Content content = contentMapper.selectContentById(id);
        r.setCode(200);
        r.setMsg("查询成功");
        r.setData(content);
        return r;
    }

    @Override
    public R updateContentById(Content content) {
        R r= new R();
        int i = contentMapper.updateContentById(content);
        if(i>0) {
            r.setCode(200);
            r.setMsg("修改成功");
        }
        return r;
    }

    @Override
    public R deleteContentById(int id) {
        R r= new R();
        int i = contentMapper.deleteContentById(id);
        if(i>0)
        {
            r.setCode(200);
            r.setMsg("删除成功");
        }
        return r;
    }

    @Override
    public R deleteContentByPatch(int[] ids) {
        R r= new R();
        List<Integer> list = new ArrayList<>();
        if(ids ==null||ids.length==0)
        {
            r.setCode(500);
            r.setData("未勾选需要删除的条目！");
        }else
        {
            for (int i=0;i<ids.length;i++)
            {
                list.add(ids[i]);
            }
            int res = contentMapper.deleteContentByPatch(list);
            if(res>0)
            {
                r.setCode(200);
                r.setMsg("删除成功");
            }
        }
        return r;
    }
}
